/*
 * Copyright Hangzhou Drore Information and Technology CD.LTD All rights reserved.
 * 
 * 杭州卓锐信息技术有限公司版权所有
 */

package com.example.handlerthread.demo;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.HandlerThread;
import android.util.Log;


/**
 * 描述：
 * 
 */
public class HandlerThreadActivity extends Activity {
    
    private static final String TAG = "HandlerThreadActivity";    
    private int count = 0;    
    private Handler mHandler ;    
        
    private Runnable mRunnable = new Runnable() {    
            
        public void run() {    
            //为了方便 查看，我们用Log打印出来    
            Log.e(TAG, Thread.currentThread().getId() + " " +count);    
            count++;    
//            setTitle("" +count);    
            //每2秒执行一次    
            mHandler.postDelayed(mRunnable, 2000);    
        }    
            
    };    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.e(TAG, "Main id    " + Thread.currentThread().getId() + " " + count);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 通过Handler启动线程
        HandlerThread handlerThread = new HandlerThread("threadone");
        handlerThread.start();
        mHandler = new Handler(handlerThread.getLooper());
        mHandler.post(mRunnable);

    }

    @Override
    protected void onDestroy() {
        // 将线程与当前handler解除
        mHandler.removeCallbacks(mRunnable);
        super.onDestroy();
    }   
}
